<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="支付方式 0 微信 1 支付宝" prop="payWay">
        <el-input v-model="queryParams.payWay" placeholder="请输入支付方式 0 微信 1 支付宝" clearable
           />
      </el-form-item>
      <el-form-item label="商户号 微信必填 支付宝无需填写" prop="mchId">
        <el-input v-model="queryParams.mchId" placeholder="请输入商户号 微信必填 支付宝无需填写" clearable
           />
      </el-form-item>
      <el-form-item label="商户序列号 微信必填 支付宝无需填写" prop="mchSerialNumber">
        <el-input v-model="queryParams.mchSerialNumber" placeholder="请输入商户序列号 微信必填 支付宝无需填写" clearable
           />
      </el-form-item>
      <el-form-item label="商户密钥 微信必填 支付宝无需填写" prop="api3Key">
        <el-input v-model="queryParams.api3Key" placeholder="请输入商户密钥 微信必填 支付宝无需填写" clearable
           />
      </el-form-item>
      <el-form-item label="微信 appid / 支付宝 appid 必填" prop="appId">
        <el-input v-model="queryParams.appId" placeholder="请输入微信 appid / 支付宝 appid 必填" clearable
           />
      </el-form-item>
      <el-form-item label="回调url 必填" prop="notifyUrl">
        <el-input v-model="queryParams.notifyUrl" placeholder="请输入回调url 必填" clearable
           />
      </el-form-item>
      <el-form-item label="支付宝专用 回调url" prop="returnUrl">
        <el-input v-model="queryParams.returnUrl" placeholder="请输入支付宝专用 回调url" clearable
           />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:payConfig:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:payConfig:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:payConfig:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:payConfig:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column
      label="支付方式"
      align="center"
      prop="payWay"
    >
      <template slot-scope="scope">
        <div>
          <img
          style="width: 40px; height: 40px; margin: 0 auto; display: block;"
            v-if="scope.row.payWay === 0"
            src="https://tse1-mm.cn.bing.net/th/id/OIP-C.aS3pq9lLxqQIzlWR4EQDsQHaG3?w=201&h=186&c=7&r=0&o=5&dpr=1.3&pid=1.7"
            alt="微信支付"
            class="payment-icon"
          />
          <img
           style="width: 40px; height: 40px; margin: 0 auto; display: block;"
            v-if="scope.row.payWay === 1"
            src="https://ts1.cn.mm.bing.net/th?id=OIP-C.4F-30SxS_AjtC4PMmq-qcAHaHa&w=250&h=250&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2"
            alt="支付宝支付"
            class="payment-icon"
          />
          <!-- 或使用图标库 -->
          <!--
          <i
            v-if="scope.row.payWay === 0"
            class="fa fa-weixin payment-icon"
            aria-hidden="true"
          ></i>
          <i
            v-if="scope.row.payWay === 1"
            class="fa fa-alipay payment-icon"
            aria-hidden="true"
          ></i>
          -->
        </div>
      </template>
    </el-table-column>
      <el-table-column label="商户号 微信必填 支付宝无需填写" align="center" prop="mchId" />
      <el-table-column label="商户序列号 微信必填 支付宝无需填写" align="center" prop="mchSerialNumber" />
      <el-table-column label="商户密钥 微信必填 支付宝无需填写" align="center" prop="api3Key" />
      <el-table-column label="微信 appid / 支付宝 appid 必填" align="center" prop="appId" />
      <el-table-column label="回调url 必填" align="center" prop="notifyUrl" />
      <el-table-column label="微信公钥/支付宝公钥" align="center" prop="publicKey">
        <template slot-scope="scope">
          <div>
            <span class="truncate-text">{{ scope.row.publicKey.substring(0, 20) }}...</span>
            <el-button @click="showFullPublicKey(scope.row.publicKey)">查看完整</el-button>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="微信私钥/支付宝私钥" align="center" prop="privateKey">
        <template slot-scope="scope">
          <div>
            <span class="truncate-text">{{ scope.row.privateKey.substring(0, 20) }}...</span>
            <el-button @click="showFullPrivateKey(scope.row.privateKey)">查看完整</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="支付宝专用 回调url" align="center" prop="returnUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:payConfig:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:payConfig:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改支付配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="支付方式 0 微信 1 支付宝" prop="payWay">
          <el-input v-model="form.payWay" placeholder="请输入支付方式 0 微信 1 支付宝" />
        </el-form-item>
        <el-form-item label="商户号 微信必填 支付宝无需填写" prop="mchId">
          <el-input v-model="form.mchId" placeholder="请输入商户号 微信必填 支付宝无需填写" />
        </el-form-item>
        <el-form-item label="商户序列号 微信必填 支付宝无需填写" prop="mchSerialNumber">
          <el-input v-model="form.mchSerialNumber" placeholder="请输入商户序列号 微信必填 支付宝无需填写" />
        </el-form-item>
        <el-form-item label="商户密钥 微信必填 支付宝无需填写" prop="api3Key">
          <el-input v-model="form.api3Key" placeholder="请输入商户密钥 微信必填 支付宝无需填写" />
        </el-form-item>
        <el-form-item label="微信 appid / 支付宝 appid 必填" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入微信 appid / 支付宝 appid 必填" />
        </el-form-item>
        <el-form-item label="回调url 必填" prop="notifyUrl">
          <el-input v-model="form.notifyUrl" placeholder="请输入回调url 必填" />
        </el-form-item>
        <el-form-item label="微信公钥 支付宝公钥 必填" prop="publicKey">
          <el-input v-model="form.publicKey" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="微信私钥 支付宝私钥 必填" prop="privateKey">
          <el-input v-model="form.privateKey" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支付宝专用 回调url" prop="returnUrl">
          <el-input v-model="form.returnUrl" placeholder="请输入支付宝专用 回调url" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 公钥对话框 -->
    <el-dialog title="完整公钥" :visible.sync="dialogVisiblePublicKey" width="50%">
      <pre class="dialog-content">{{ fullPublicKey }}</pre>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblePublicKey = false">关闭</el-button>
      </span>
    </el-dialog>

    <!-- 私钥对话框 -->
    <el-dialog title="完整私钥" :visible.sync="dialogVisiblePrivateKey" width="50%">
      <pre class="dialog-content">{{ fullPrivateKey }}</pre>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblePrivateKey = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listPayConfig, getPayConfig, delPayConfig, addPayConfig, updatePayConfig } from "@/api/system/payConfig.js";

export default {
  name: "PayConfig",
  data() {
    return {
      dialogVisiblePublicKey: false,
      dialogVisiblePrivateKey: false,
      fullPublicKey: '',
      fullPrivateKey: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付配置表格数据
      payConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payWay: null,
        mchId: null,
        mchSerialNumber: null,
        api3Key: null,
        appId: null,
        notifyUrl: null,
        publicKey: null,
        privateKey: null,
        returnUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        payWay: [
          { required: true, message: "支付方式 0 微信 1 支付宝不能为空", trigger: "blur" }
        ],
        appId: [
          { required: true, message: "微信 appid / 支付宝 appid 必填不能为空", trigger: "blur" }
        ],
        notifyUrl: [
          { required: true, message: "回调url 必填不能为空", trigger: "blur" }
        ],
        publicKey: [
          { required: true, message: "微信公钥 支付宝公钥 必填不能为空", trigger: "blur" }
        ],
        privateKey: [
          { required: true, message: "微信私钥 支付宝私钥 必填不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    showFullPublicKey(publicKey) {
      this.fullPublicKey = publicKey;
      this.dialogVisiblePublicKey = true;
    },
    showFullPrivateKey(privateKey) {
      this.fullPrivateKey = privateKey;
      this.dialogVisiblePrivateKey = true;
    },
    /** 查询支付配置列表 */
    getList() {
      this.loading = true;
      listPayConfig(this.queryParams).then(response => {
        this.payConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        payWay: null,
        mchId: null,
        mchSerialNumber: null,
        api3Key: null,
        appId: null,
        notifyUrl: null,
        publicKey: null,
        privateKey: null,
        returnUrl: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPayConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改支付配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePayConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除支付配置编号为"' + ids + '"的数据项？').then(function () {
        return delPayConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/payConfig/export', {
        ...this.queryParams
      }, `payConfig_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.truncate-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.dialog-content {
  white-space: pre-wrap; /* 保留换行符，并允许内容换行 */
  word-wrap: break-word; /* 允许单词在换行处断开 */
  overflow: auto; /* 添加滚动条以处理超出内容 */
}
</style>